package com.zxx.casserver.modules.role.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxx.cascommon.base.exception.ParamsException;
import com.zxx.cascommon.base.result.Result;
import com.zxx.cascommon.base.result.ResultUtils;
import com.zxx.casserver.modules.oplog.aop.OpLogAnnotation;
import com.zxx.casserver.modules.role.entity.RoleDO;
import com.zxx.casserver.modules.role.entity.RoleVO;
import com.zxx.casserver.modules.role.service.IRoleService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色管理-前端控制器
 *
 * @author zhoukai
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    private final IRoleService roleService;

    public RoleController(IRoleService roleService) {
        this.roleService = roleService;
    }

    @PostMapping("/list")
    public Result listRole(@RequestBody RoleVO roleVO) {
        IPage<RoleDO> userPage = roleService.listRole(roleVO);
        if (ObjectUtils.isEmpty(userPage)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(userPage);
    }


    @PostMapping("/get")
    public Result getRole(@RequestBody RoleVO roleVO) {
        if (ObjectUtils.isEmpty(roleVO.getRoleId())) {
            throw new ParamsException();
        }
        RoleVO roleBO = roleService.getRole(roleVO);
        if (ObjectUtils.isEmpty(roleBO)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(roleBO);
    }


    @OpLogAnnotation(modules = "角色",
            action = "保存", msg = "角色保存")
    @PostMapping("/save")
    public Result saveRole(@RequestBody RoleVO roleVO) {
        if (ObjectUtils.isEmpty(roleVO.getName())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(roleVO.getRolePermissionList())) {
            throw new ParamsException();
        }
        RoleDO roleDO = roleService.saveRole(roleVO);
        if (ObjectUtils.isEmpty(roleDO)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(roleDO);
    }

    @OpLogAnnotation(modules = "角色",
            action = "更新", msg = "角色更新")
    @PostMapping("/update")
    public Result updateRole(@RequestBody RoleVO roleVO) {
        if (ObjectUtils.isEmpty(roleVO.getRoleId())) {
            throw new ParamsException();
        }
        RoleDO roleDO = roleService.updateRole(roleVO);
        if (ObjectUtils.isEmpty(roleDO)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(roleDO);
    }

    @OpLogAnnotation(modules ="角色",
            action = "删除", msg = "角色删除")
    @PostMapping("/remove")
    public Result removeRole(@RequestBody RoleVO roleVO) {
        if (ObjectUtils.isEmpty(roleVO.getRoleId())) {
            throw new ParamsException();
        }
        RoleDO roleDO = roleService.removeRole(roleVO);
        if (ObjectUtils.isEmpty(roleDO)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(roleDO);
    }
}

